<template>
  <div>
    <!-- 顶部导航 -->
    <van-nav-bar fixed>
      <template #left>
        <img :src="imgObj" alt="" class="logo" />
      </template>
      <template #right>
        <van-icon name="search" size="18" color="#fff" @click="$router.push('/search')"/>
      </template>
    </van-nav-bar>
    <!-- 频道部分 -->
    <van-tabs v-model="active" animated sticky offset-top="1.2266rem">
      <van-tab
        v-for="item in channeList"
        :title="item.name"
        :name="item.id"
        :key="item.id"
      >
        <article-list :cal="active"></article-list>
      </van-tab>
    </van-tabs>
    <!-- 编辑频道图标 -->
    <van-icon
      name="plus"
      size="0.37333334rem"
      class="moreChannels"
      @click="show = true"
    />
    <!-- 弹出层组件 -->
    <van-popup v-model="show" class="edit_wrap" @closed="onPopupClosed">
      <!-- 弹出层的主体区域 -->
      <channel-edit
        :userChannelList="channeList"
        @close="show = false"
        @addChannel="addChannelFn"
        @removeChannel="removeChannelFn"
        @changeChannel="changeChannelFn"
        ref="ChannelEdit"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
// 引入图片变量
import logoPng from '../../assets/toutiao_logo.png'
import { userChannelAPI, updateChannelAPI } from '@/api'
import ArticleList from './ArticleList.vue'
import ChannelEdit from './ChannelEdit.vue'
export default {
  components: { ArticleList, ChannelEdit },
  data () {
    return {
      imgObj: logoPng,
      active: 0,
      channeList: [],
      show: false // 编辑频道弹出层显示
    }
  },
  async created () {
    const res = await userChannelAPI()
    // 我的频道数据
    this.channeList = res.data.data.channels
  },
  methods: {
    // 自定义事件点击编辑添加频道
    async addChannelFn (item) {
      this.channeList.push(item)
      this.updateChannel()
    },
    // 自定义事件点击编辑删除频道
    removeChannelFn (val) {
      // findIndex方法用于在数组中查找当前项的索引，如果有返回索引值，没有返回-1
      const index = this.channeList.findIndex((item) => item.id === val.id)
      this.channeList.splice(index, 1)
      this.updateChannel()
    },
    // 封装一个接口调用方法
    async updateChannel () {
      // 编辑的时候去掉编辑频道
      // 遍历去掉频道编辑的数组
      // 进行浅拷贝
      // 将seq添加到每一个对象中 seq是当前的顺序 index + 1
      // 删除对象中的name
      const list = this.channeList.filter((item) => {
        return item.id !== 0
      })
      const resArr = list.map((item, index) => {
        const newObj = { ...item }
        newObj.seq = index + 1
        delete newObj.name
        return newObj
      })
      // 调用接口
      await updateChannelAPI({
        channels: resArr
      })
    },
    // 弹层关闭-重置编辑状态
    onPopupClosed () {
      this.$refs.ChannelEdit.isEdit = false
    },
    // 切换频道
    changeChannelFn (val) {
      // 传过来的频道ID, 影响tabs默认v-model的选择
      this.active = val.id
    }
  }
}
</script>

<style scoped lang="less">
.logo {
  width: 100px;
  height: 30px;
}
/deep/ .van-tabs__content {
  padding-top: 44px;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 60.5px;
  right: 8px;
  z-index: 999;
}
/* 频道编辑弹出层铺满屏幕 */
.edit_wrap {
  width: 100%;
  height: 100%;
}
</style>
